home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Swar / shots.c < prev    next >
Text File  |  1994-08-21  |  3KB  |  109 lines

  1.  
  2. #include "swar.h"
  3.  
  4. InitShots()
  5. {
  6.     extern RGBColor    myWhite, myBlack, myRed, myBlue, myYellow, myGreen, myOrange, myGray, myDkBlue;
  7.     extern SHOTREC        gShotRecs[MAX_SHOTS], gOldShotRecs[MAX_SHOTS];
  8.     short                    i, nc;
  9.     
  10.     for (i = 0; i < MAX_SHOTS; i++) {
  11.         gShotRecs[i].where.v = -1;
  12.         gShotRecs[i].where.h = -1;
  13.         gShotRecs[i].vel.v = -1;
  14.         gShotRecs[i].vel.h = -1;
  15.         gShotRecs[i].lifeSpan = 0;
  16.         gShotRecs[i].isAlive = FALSE;
  17.     } /* for */
  18.     
  19.  
  20. } /* InitShots() */
  21.  
  22. CreateShot(c, h, v, hv, vv)
  23.     RGBColor    c;
  24.     short        h, v, hv, vv;
  25. {
  26.     short        i;
  27.     
  28.     for (i = 0; i < MAX_SHOTS; i++)
  29.         if (!gShotRecs[i].isAlive) {
  30.             gShotRecs[i].where.v = v;
  31.             gShotRecs[i].where.h = h;
  32.             gShotRecs[i].vel.v = vv;
  33.             gShotRecs[i].vel.h = hv;
  34.             gShotRecs[i].isAlive = TRUE;
  35.             gShotRecs[i].lifeSpan = 20;
  36.             gShotRecs[i].color = c;
  37.             return;
  38.         } /* if */
  39.         
  40.     
  41. } /* CreateShot() */
  42.  
  43. MoveShots()
  44. {
  45.     short    i;
  46.     extern SHOTREC        gShotRecs[MAX_SHOTS];
  47.  
  48.     for (i = 0; i < MAX_SHOTS; i++)
  49.         if (gShotRecs[i].isAlive) {
  50.             if (gShotRecs[i].lifeSpan) {
  51.                 gShotRecs[i].lifeSpan--;
  52.                 gShotRecs[i].where.h += gShotRecs[i].vel.h;
  53.                 gShotRecs[i].where.v += gShotRecs[i].vel.v;
  54.                 if (gShotRecs[i].where.h > 637) {
  55.                     gShotRecs[i].vel.h *= -1;
  56.                     gShotRecs[i].where.h = 637;
  57.                 } /* if */
  58.                 if (gShotRecs[i].where.h < 1) {
  59.                     gShotRecs[i].vel.h *= -1;
  60.                     gShotRecs[i].where.h = 1;
  61.                 } /* if */
  62.                 if (gShotRecs[i].where.v > 477) {
  63.                     gShotRecs[i].vel.v *= -1;
  64.                     gShotRecs[i].where.v = 477;
  65.                 } /* if */
  66.                 if (gShotRecs[i].where.v < 21) {
  67.                     gShotRecs[i].vel.v *= -1;
  68.                     gShotRecs[i].where.v = 21;
  69.                 } /* if */
  70.             } /* if */
  71.             else
  72.                 gShotRecs[i].isAlive = FALSE;
  73.         } /* if */
  74.     
  75. } /* MoveShots() */
  76.  
  77. DrawShots()
  78. {
  79.     GrafPtr    savePort;
  80.     extern CGrafPort    *gOSPtr;
  81.     short                i, j;
  82.     extern RGBColor    myBlack;
  83.     Rect                dstRect;
  84.     extern SHIPREC        gShipRecs[MAX_PLAYERS];
  85.     
  86.     GetPort(&savePort);
  87.     SetPort((GrafPtr)gOSPtr);
  88.     for (i = 0; i < MAX_SHOTS; i++) {
  89.         if (gOldShotRecs[i].isAlive)
  90.             SetCPixel(gOldShotRecs[i].where.h, gOldShotRecs[i].where.v, &myBlack);
  91.         if (gShotRecs[i].isAlive) {
  92.             SetCPixel(gShotRecs[i].where.h, gShotRecs[i].where.v, &(gShotRecs[i].color));
  93.             gOldShotRecs[i] = gShotRecs[i];
  94.             dstRect.top = gShotRecs[i].where.v;
  95.             dstRect.left = gShotRecs[i].where.h;
  96.             dstRect.bottom = dstRect.top + 1;
  97.             dstRect.right = dstRect.left + 1;
  98.             for (j = 0; j < MAX_PLAYERS; j++)
  99.                 if (!ColorsEqual(gShotRecs[i].color, gShipRecs[j].color))
  100.                     if (CheckForShipCollision(j, dstRect)) {
  101.                         gShotRecs[i].isAlive = FALSE;
  102.                         KillPlayer(j);
  103.                     } /* if */
  104.         } /* if */
  105.     } /* for */
  106.     SetPort((GrafPtr)savePort);
  107.             
  108. } /* DrawShots() */
  109.